import 'dart:isolate';

main() async {
  print("start");
  Isolate.spawn(calculate, 1000);
  Isolate.spawn(calculate, 100);
//  1.创建管道
  ReceivePort receivePort = ReceivePort();
//  2.创建isolate
  Isolate isolate = await Isolate.spawn<SendPort>(foo, receivePort.sendPort);
//  3.监听管道
  receivePort.listen((message) {
    print(message);
    receivePort.close();
    isolate.kill();
  });
  print("end");
}

void calculate(int count) {
  var total = 0;
  for (var i = 0; i < count; i++) {
    total += i;
  }
  print(total);
}

void foo(SendPort send) {
  return send.send("hello world");
}
